<!--
  - Copyright (c) 2019.  武汉中科图灵科技有限公司
  - Date :  2019/1/5
  - Author ：F12 Console=> atob("MzMxNDkxODAwQHFxLmNvbQ==")
  - Version ：1.6.2
  -->

<template>
  <div><div class="abstatic-height"></div>
  <div class="app-container calendar-list-container">
    <el-row>
      <div class="xf-tabletop-handles">
        <div class="xf-tabhd-left">
          <el-button class="filter-item" @click="handleCreate" type="primary" size="mini">添加等级</el-button>
        </div>
      </div>
    </el-row>
    <el-table :key='tableKey' :data="list" v-loading.body="listLoading" style="width: 100%">
      <el-table-column label="序号" width="200">
        <template slot-scope="scope">
          <span>{{scope.row.sort}}</span>
        </template>
      </el-table-column>
      <el-table-column label="报警等级">
        <template slot-scope="scope">
          <span>{{scope.row.level}}</span>
        </template>
      </el-table-column>
      <el-table-column label="等级说明">
        <template slot-scope="scope">
          <span>{{scope.row.levelInstructions}}</span>
        </template>
      </el-table-column>
      <el-table-column label="报警颜色">
        <template slot-scope="scope">
          <span :style="{'background':scope.row.color}" class="alarm-bg-color"></span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <span @click="handleUpdate(scope.row)" class="comm-table-btn"><icon-svg icon-class="bianji" style="fill:#50E3C2" /></span>
          <span @click="handleDelete(scope.row)" class="comm-table-btn"><icon-svg icon-class="shanchu" style="fill:#50E3C2" /></span>
        </template>
      </el-table-column>
    </el-table>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
    </div>
    <slider-panel :Show.sync="dialogFormVisible" custom-class="out-side-frame">
      <div class="form-content-frame">
        <el-form :model="form" :rules="rules" ref="form" label-width="100px" label-position='top' @submit.native.prevent>
          <el-form-item label="报警等级" prop="level">
            <el-input v-model.trim="form.level" placeholder="请输入报警等级"></el-input>
          </el-form-item>
          <el-form-item label="等级说明" prop="levelInstructions">
            <el-input v-model.trim="form.levelInstructions" placeholder="请输入等级说明"></el-input>
          </el-form-item>
          <el-form-item label="报警颜色" prop="color">
            <el-input v-model.trim="form.color" placeholder="请选择报警颜色" :disabled="true"></el-input>
            <div class="color-box">
              <el-color-picker v-model="form.color">
              </el-color-picker>
              <span>{{dialogStatus=='create'?'选择颜色':'修改颜色'}}</span>
            </div>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input v-model.number="form.sort" placeholder="请输入排序"></el-input>
          </el-form-item>
            <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')" size="mini" native-type="submit">确 定</el-button>
            <el-button v-else type="primary" @click="update('form')" size="mini" native-type="submit">确 定</el-button>
            <el-button @click="cancel('form')" type="text" size="mini">取 消</el-button>
        </el-form>
      </div>
    </slider-panel>
    <el-dialog title="删除报警等级" :visible.sync="dialogDeleteVisible" :show-close='false' custom-class="xf-jc-dialog" width="360px">
      <div class="xf-jc-deletect">
        <p>删除此等级将会导致子站的报警通知同步删除，请谨慎操作</p>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" size="mini"  @click="deleteD(currentSelectObj)">删除</el-button>
        <el-button type="text" size="mini"  @click="dialogDeleteVisible = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
  </div>
</template>

<script>

import { validatInter } from "@/utils/validate";
import {
  page,
  addObj,
  getObj,
  delObj,
  putObj
} from "@/api/device/deviceAlarmLevel/index";
import { mapGetters } from "vuex";
import sliderPanel from '@/components/SliderPanel'
export default {
  name: "deviceAlarmLevel",
  components: {sliderPanel},
  data() {
     const validateInterg = (rule, value, callback) => {
      if (!validatInter(value)) {
        callback(new Error("请输入大于0的整数"));
      }else if((value+'').length>5){
        callback(new Error("最多允许输入5个字符"));
      } else {
        callback();
      }
    };
    return {
      form: {
        level: undefined,
        color: null,
        sort: undefined,
        levelInstructions: undefined
      },
      rules: {
        level: [
          {
            required: true,
            message: "请输入报警等级",
            trigger: "blur"
          },
          {
            min:1,
            max: 10,
            message: "最多允许输入10个字符",
            trigger: "blur"
          }
        ],
        color: [
          {
            required: true,
            message: "请选择报警颜色",
            trigger: "change"
          },
        ],
        levelInstructions: [
          {
            required: true,
            message: "请输入等级说明",
            trigger: "blur"
          },
          {
            min:1,
            max: 50,
            message: "最多允许输入50个字符",
            trigger: "blur"
          }
        ],
        sort: [
          {
            required: true,
            trigger: "blur",
            validator: validateInterg
          }
        ]
      },
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        name: undefined
      },
      dialogFormVisible: false,
      dialogDeleteVisible:false,
      currentSelectObj:{},
      dialogStatus: "",
      textMap: {
        update: "编辑报警等级",
        create: "添加报警等级"
      },
      tableKey: 0
    };
  },
  created() {
    this.getList();
  },
  computed: {
    ...mapGetters(["elements"])
  },
  methods: {
    getList() {
      this.listLoading = true;
      page(this.listQuery).then(response => {

        this.list = response.data.rows;

        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    handleFilter() {
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleCreate() {
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
    },
    handleUpdate(row) {
      getObj(row.id).then(response => {
        this.form = response.data;
        this.dialogFormVisible = true;
        this.dialogStatus = "update";
      });
    },
    deleteD(row){
      this.dialogDeleteVisible = false;
      delObj(row.id).then(() => {
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
            duration: 2000
          });
          this.getList();
        });
    },
    handleDelete(row) {
      this.currentSelectObj = row;
      this.dialogDeleteVisible = true;
    },
    create(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          addObj(this.form).then(() => {
            this.dialogFormVisible = false;

            this.resetTemp();
            this.$refs[formName].resetFields();
            this.getList();
            this.$notify({
              title: "成功",
              message: "创建成功",
              type: "success",
              duration: 2000
            });
          });
        } else {
          return false;
        }
      });
    },
    cancel(formName) {
      this.dialogFormVisible = false;
      this.resetTemp();
      this.$refs[formName].resetFields();
    },
    update(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false;
          putObj(this.form).then(() => {
            this.dialogFormVisible = false;
            this.resetTemp();
            this.$refs[formName].resetFields();

            this.getList();
            this.$notify({
              title: "成功",
              message: "修改成功",
              type: "success",
              duration: 2000
            });
          });
        } else {
          return false;
        }
      });
    },
    resetTemp() {
      this.form = {
        level: undefined,
        color: null,
        sort: undefined,
        levelInstructions: undefined
      };
    }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss">
.alarm-bg-color{
  display: inline-block;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  vertical-align: middle;
}
.el-table th {
  background: #f5f7fa;
}
.xf-query-input,
.xf-query-select {
  width: 200px;
  margin: 0 10px;
}
.fulldialog > div {
  width: 500px;
  margin: 0 auto;
}
.fulldialog .el-dialog__title {
  font-size: 32px;
}
.fulldialog {
  .el-dialog__header {
    margin-top: 20px;
  }
  .custom-dialog-btn {
    position: absolute;
    font-size: 30px;
    left: 40px;
    top: 20px;
    color: #fff;
    &:hover {
      cursor: pointer;
    }
  }
  .dialog-footer {
    text-align: left;
  }
}

.el-input {
  position: relative;
}
.color-box {
  height: 36px;
  width: 143px;
  background-color:rgba(80, 227, 194,.3);
  border: 1px solid transparent;
  border-radius: 0 4px 4px 0;
  position: absolute;
  top: 0;
  right: 0;
}
.el-color-picker--medium {
  vertical-align: middle;
}
.el-color-picker__trigger {
  border: none;
  top: -2px;
}
</style>

